package com.extension.company.view.income;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseActivity;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.IncomeDetailContract;
import com.extension.company.presenter.income.IncomeDetailPresenter;
import com.extension.company.view.adapter.IncomeDetailAdapter;
import com.jaeger.library.StatusBarUtil;

import butterknife.BindView;

public class IncomeDetail extends BaseActivity<IncomeDetailContract.View, IncomeDetailPresenter>
        implements IncomeDetailContract.View, View.OnClickListener {

    @BindView(R.id.m_top)
    LinearLayout m_top;

    @BindView(R.id.m_back)
    ImageView m_back;

    @BindView(R.id.mListView)
    ListView mListView;

    @BindView(R.id.layout_title)
    RelativeLayout layout_title;

    private View headerview;

    private IncomeDetailAdapter adapter;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, IncomeDetail.class);
        activity.startActivity(intent);
    }

    @Override
    public int getMainLayout() {
        return R.layout.incomedetail;
    }

    @Override
    protected void initData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
            layoutParams.height = Utils.getStatusBarHeight(this);
            m_top.setLayoutParams(layoutParams);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    |View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            m_top.setBackgroundColor(getResources().getColor(R.color.white));
        }else {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.black));
            m_top.setVisibility(View.GONE);
        }
        headerview = LayoutInflater.from(this).inflate(R.layout.incomedetail_headview, null);
        mListView.addHeaderView(headerview);
        adapter = new IncomeDetailAdapter(this);
        mListView.setAdapter(adapter);
        m_back.setAlpha(1.0f);
        m_top.setAlpha(1.0f);
    }

    @Override
    protected void initListener() {
        m_back.setOnClickListener(this);
        mListView_OnScrollListener();
    }

    private void mListView_OnScrollListener() {
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                float abs = Math.abs(headerview.getTop());
                int height = layout_title.getHeight();
                if (abs == 0.0f) {
                    layout_title.setAlpha(0.0f);
                    m_top.setAlpha(0.0f);
                    m_back.setImageResource(R.drawable.fanh_white);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        IncomeDetail.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                |View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                        m_top.setBackgroundColor(getResources().getColor(R.color.white));
                    }
                }else {
                    float i = 1.0f / height * abs;
                    if (i < 1.0f) {
                        layout_title.setAlpha(i);
                        m_top.setAlpha(i);
                    }else {
                        m_back.setImageResource(R.drawable.fanh);
                        layout_title.setAlpha(1.0f);
                        m_top.setAlpha(1.0f);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            IncomeDetail.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                    |View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                            m_top.setBackgroundColor(getResources().getColor(R.color.white));
                        }
                    }
                }
            }
        });
    }

    @Override
    public IncomeDetailContract.View createView() {
        return this;
    }

    @Override
    public IncomeDetailPresenter createPresenter() {
        return new IncomeDetailPresenter();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m_back:
                finish();
                break;
        }
    }
}
